package com.baizhi.controller;

import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 野马 on 2019/6/19.
 * 轮播图
 */
@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    //添加
    @RequestMapping("/insertBanner")
    @ResponseBody
    public Map insert(String bannerOldname, String bannerDescription, MultipartFile touxiang, HttpServletRequest request){
        //获取源文件的名字
        String filename = touxiang.getOriginalFilename();

        String str = UUID.randomUUID().toString();
        //把源文件的名字换成uuid新名字   获取最后一个.的下标    从这个下标开始截取
        String newName = str + filename.substring(filename.lastIndexOf("."));

        Banner banner = new Banner();
        banner.setBannerOldname(bannerOldname);
        banner.setBannerDescription(bannerDescription);
        banner.setBannerImageurl(newName);


        Map map = new HashMap();

        //把接收的文件写入到服务器
        try {
            String realPath = request.getRealPath("img");
            File file = new File(realPath+"/"+newName);
            touxiang.transferTo(file);
            bannerService.insertBanner(banner);
            map.put("insertBaanner",true);
        } catch (IOException e) {
            map.put("insertBanner",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("deleteBanner")
    @ResponseBody
    public Map delete(int bannerId){
        Map map = new HashMap();
        try {
            bannerService.deleteBanner(bannerId);
            map.put("bannerdelete",true);
        } catch (Exception e) {
            map.put("bannerdelete",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("updateBanner")
    @ResponseBody
    public Map update(String bannerOldname, String bannerDescription, MultipartFile touxiang,HttpServletRequest request){

        //获取源文件的名字
        String filename = touxiang.getOriginalFilename();
        String str = UUID.randomUUID().toString();

        //把原名字换成uuid新名字
        String newName = str + filename.substring(filename.lastIndexOf("."));

        Banner banner = new Banner();
        banner.setBannerOldname(bannerOldname);
        banner.setBannerDescription(bannerDescription);
        banner.setBannerImageurl(newName);

        //把文件写入磁盘
        Map map = new HashMap();
        try {
            String realPath = request.getRealPath("img");
            File file = new File(realPath+newName);
            touxiang.transferTo(file);
            bannerService.updateBanner(banner);
            map.put("bannerupdate",true);
        } catch (IOException e) {
            map.put("bannerupdate",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("selectBanner")
    @ResponseBody
    public Map select(int page,int rows){
        return bannerService.selectBanner(page, rows);
    }





}
